//
//  MoreListViewController.m
//  XMLY
//
//  Created by 曾杰 on 15/11/6.
//  Copyright © 2015年 曾杰. All rights reserved.
//

#import "MoreListViewController.h"
#import "PKRequestManager.h"
#import "SoundBookCell.h"
#import "SoundBookModel.h"
#import "MJRefresh.h"
#import "UIImageView+WebCache.h"
#import "MusicDetailViewController.h"
@interface MoreListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UISegmentedControl *segment;
@property (nonatomic, strong)UITableView *morelistTable;
@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, strong)NSString *calc;
@property (nonatomic, assign)NSInteger pageId;
@end

@implementation MoreListViewController

-(NSMutableArray *)dataArr
{
    if (_dataArr == nil) {
        self.dataArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataArr;
}

-(void)requestData
{
    

    NSString * urlStr = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/category/album?calcDimension=%@&categoryId=%@&device=iPhone&pageId=%ld&pageSize=20&position=1&status=0&tagName=%@",self.calc,self.categoryId,(long)self.pageId,self.tagName];
    NSLog(@"******************%@",urlStr);
        [PKRequestManager requestWithType:GET urlString:urlStr parDic:nil finish:^(NSData *data) {
        
           
                NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
          
                NSArray *arr = dic[@"list"];
                NSString * totalCount = dic[@"totalCount"];
                NSLog(@"&&&&&&&&&%@",totalCount);
                for (NSDictionary *listDic in arr) {
                    SoundBookModel *model = [[SoundBookModel alloc]init];
                    [model setValuesForKeysWithDictionary:listDic];
                    [model setValue:totalCount forKey:@"totalCount"];
                    [self.dataArr addObject:model];
                }
                [self.morelistTable.header endRefreshing];
                [self.morelistTable.footer endRefreshing];
                [self.morelistTable reloadData];
     
    } error:^(NSError *error) {
        
    }];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    _calc = @"hot";
    _pageId = 1;
    self.view.backgroundColor = [UIColor whiteColor];
    //创建顶部视图
    [self topView];
    
    [self requestData];
    
    //创建segment
    [self createSegment];
    //创建tableview
    [self createMoreListTable];
    //创建刷新
    [self createRefresh];
}
//创建顶部视图
- (void)topView
{
    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-100, 20, 200, 30)];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor orangeColor];
    titleLabel.text = _titleStr;
    [topView addSubview:titleLabel];
    [self.view addSubview:topView];
    UIButton *returnButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    returnButton.frame = CGRectMake(10, 25, 20, 20);
    [returnButton addTarget:self action:@selector(didClickReturn:) forControlEvents:(UIControlEventTouchUpInside)];
    [returnButton setImage:[UIImage imageNamed:@"iconfont-fanhui (1)"] forState:(UIControlStateNormal)];
    [topView addSubview:returnButton];
}

//创建segment
- (void)createSegment
{
    NSArray *arr = @[@"最火",@"最近更新",@"经典"];
    _segment = [[UISegmentedControl alloc]initWithItems:arr];
    _segment.frame = CGRectMake(0, 64, SCREEN_WIDTH, 30);
    _segment.selectedSegmentIndex = 0;
    _segment.tintColor = [UIColor orangeColor];
    [_segment addTarget:self action:@selector(didClickSegment:) forControlEvents:(UIControlEventValueChanged)];
    [self.view addSubview:_segment];
}
//创建tableview
- (void)createMoreListTable
{
    _morelistTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 94, SCREEN_WIDTH, SCREEN_HEIGHT-94)];
    _morelistTable.delegate = self;
    _morelistTable.dataSource = self;
    [_morelistTable registerClass:[SoundBookCell class] forCellReuseIdentifier:@"moreListCell"];
    [self.view addSubview:_morelistTable];
}
//创建刷新
- (void)createRefresh
{
    self.morelistTable.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.dataArr removeAllObjects];
        _pageId = 1;
        [self requestData];
    }];
    self.morelistTable.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _pageId += 1;
        [self requestData];
    }];

}

//返回按钮
- (void)didClickReturn:(UIButton *)Button
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SoundBookCell *cell = [tableView dequeueReusableCellWithIdentifier:@"moreListCell" forIndexPath:indexPath];
    SoundBookModel *model = self.dataArr[indexPath.row];
    if (self.dataArr.count==[model.totalCount integerValue]) {
        [self.morelistTable.footer noticeNoMoreData];
    }
    
    [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:model.albumCoverUrl290]];
    cell.titleLabel.text = model.title;
    cell.introLabel.text = model.intro;
   
    if (model.playsCounts < 10000) {
        cell.playCountLabel.text = [NSString stringWithFormat:@"%.0ld",(long)model.playsCounts];
    }else
    {
        float playsCount = model.playsCounts / 10000.0;

         cell.playCountLabel.text = [[NSString stringWithFormat:@"%.1f",playsCount] stringByAppendingString:@"万"];
    }
    cell.tracksCounts.text = [[NSString stringWithFormat:@"%ld",(long)model.tracksCounts] stringByAppendingString:@"集"];
    return cell;
}

-(void)didClickSegment:(UISegmentedControl *)segment
{
    if (segment.selectedSegmentIndex == 0)
    {
        [self.dataArr removeAllObjects];
        _calc = @"hot";
        [self requestData];
    }if (segment.selectedSegmentIndex == 1)
    {
        [self.dataArr removeAllObjects];
        _calc = @"recent";
        [self requestData];
    }else
    {
        [self.dataArr removeAllObjects];
        _calc = @"mostplay";
        [self requestData];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

   MusicDetailViewController * detailVC = [[MusicDetailViewController alloc]init];
    SoundBookModel * model = self.dataArr[indexPath.row];
    detailVC.albumId =[NSString stringWithFormat:@"%ld",model.albumId];
    [self presentViewController:detailVC animated:YES completion:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
